#pragma once
#include "../common/net.hpp"
#include "../common/message.hpp"
//服务端的模块
namespace bitpr
{
    namespace server
    {
        // 参数类型
        enum class VType
        {
            BOOL = 0,
            INTEGRAL,
            NUMERIC,
            STRING,
            ARRAY,
            OBJECT
        };
        class ServiceDescribe
        {
        public:
            using ptr = std::shared_ptr<ServiceDescribe>;
            using ServiceCallback = std::function<void(const Json::Value &, Json::Value &)>;
            using ParamsDescribe = std::pair<std::string, VType>;
            //我的理解:
            //1 左值引用就是常用的引用,针对已命名对象---本质是浅拷贝
            //2 右值引用就是针对这个临时对象和这个std::move--不拷贝,只转移资源
            //2.1 拷贝构造函数就是基于右值引用,针对右值对象----不拷贝,只转移
            ServiceDescribe(std::string &&mname, std::vector<ParamsDescribe> &&desc, VType vtype, ServiceCallback &&handler) : _method_name(mname), _callback(std::move(handler)), _params_desc(std::move(desc)), _return_type(vtype)
            {
            }
            const std::string &method()
            {
                return _method_name;
            }
            // 参数校验
            bool paramCheck(const Json::Value &params)
            {

                for (auto &desc : _params_desc)
                {
                    if (params.isMember(desc.first) == false)
                    {
                        ELOG("参数字段完整性校验失败%s,字段缺失", desc.first.c_str())
                    }
                    if (check(desc.second, params[desc.first]) == false)
                    {
                        ELOG("%s参数类型校验失败", desc.first.c_str());
                        return false;
                    }
                }
                return true;
            }
            //
            bool call(const Json::Value &params, Json::Value &result)
            {
                _callback(params, result);
                if (rtypeCheck(result) == false)
                {
                    ELOG("回调处理函数中的响应信息校验失败！");
                    return false;
                }
                return true;
            }

        private:
            bool check(VType vtype, const Json::Value &val)
            {
                switch (vtype)
                {
                case VType::BOOL:
                    return val.isBool();
                case VType::INTEGRAL:
                    return val.isIntegral();
                case VType::NUMERIC:
                    return val.isNumeric();
                case VType::STRING:
                    return val.isString();
                case VType::ARRAY:
                    return val.isArray();
                case VType::OBJECT:
                    return val.isObject();
                }
                return false;
            }
            bool rtypeCheck(const Json::Value &val)
            {
                return check(_return_type, val);
            }

            std::string _method_name;                 // 方法名称
            ServiceCallback _callback;                // 实际的回调函数
            std::vector<ParamsDescribe> _params_desc; // first---名称  second--参数
            VType _return_type;                       // 返回值类型的描述
        };
        class SDescribeFactory
        {
        public:
            void setMethodName(const std::string &name)
            {
                _method_name = name;
            }
            void setReturntype(VType vtype)
            {
                _return_type = vtype;
            }
            void setParamsDesc(const std::string &pname, VType vtype)
            {
                _params_desc.push_back(ServiceDescribe::ParamsDescribe(pname, vtype));
            }
            void setCallback(const ServiceDescribe::ServiceCallback &cb)
            {
                _callback = cb;
            }
            // 返回一个指针指针
            ServiceDescribe::ptr build()
            {
                return std::make_shared<ServiceDescribe>(std::move(_method_name),
                                                         std::move(_params_desc),
                                                         _return_type,
                                                         std::move(_callback));
            }

        private:
            std::string _method_name;
            ServiceDescribe::ServiceCallback _callback;
            std::vector<ServiceDescribe::ParamsDescribe> _params_desc;
            VType _return_type;
        };
        class ServiceManager
        {
        public:
            using ptr = std::shared_ptr<ServiceManager>;
            void insert(const ServiceDescribe::ptr &desc)
            {
                std::unique_lock<std::mutex> Lock(_mutex);
                _services.insert(std::make_pair(desc->method(), desc));
            }
            void remove(const std::string &method_name)
            {
                std::unique_lock<std::mutex> Lock(_mutex);
                _services.erase(method_name);
            }
            ServiceDescribe::ptr select(const std::string &method_name)
            {
                std::unique_lock<std::mutex> lock(_mutex);
                auto it = _services.find(method_name);
                if (it == _services.end())
                {
                    return ServiceDescribe::ptr();
                }
                return it->second;
            }

        private:
            std::mutex _mutex;
            std::unordered_map<std::string, ServiceDescribe::ptr> _services;
        };
        class RpcRouter
        {
        public:
            using ptr = std::shared_ptr<RpcRouter>;
            RpcRouter() : _service_manager(std::make_shared<ServiceManager>()) {};
            // 这个是注册到这个dispacher,对这个Rpc的回调函数的业务
            void onRpcRequest(const BaseConnection::ptr &conn, RpcRequest::ptr &request)
            {
                // 查询客户端请求的方法描述---是否存在响应的服务
                auto service = _service_manager->select(request->method());
                if (service.get() == nullptr)
                {
                    ELOG("%s服务未找到", request->method().c_str());
                    return response(conn, request, Json::Value(), RCode::RCODE_NOT_FOUND_SERVICE);
                }
                // 2参数校验
                if (service->paramCheck(request->params()) == false)
                {
                    ELOG("%s服务参数校验失败", request->method().c_str());
                    return response(conn, request, Json::Value(), RCode::RCODE_INVAILD_PARAMS);
                }
                // 3调用回调函数
                Json::Value result;
                bool ret = service->call(request->params(), result);
                if (ret == false)
                {
                    ELOG("%s服务参数校验失败", request->method().c_str());
                    return response(conn, request, Json::Value(), RCode::RCODE_INTERNAL_ERROR);
                }
                ELOG("result : %d", result.asInt());
                 // 处理完返回响应,向这个客户端发送
                return response(conn, request, result, RCode::RCODE_OK);//返回值是不是出错了
            }
            void registerMethod(const ServiceDescribe::ptr &service) {
                return _service_manager->insert(service);
                }
               

        private:
            void response(const BaseConnection::ptr &conn,
                          const RpcRequest::ptr &req,
                          const Json::Value &res, RCode rcode)
            {   
                auto msg = MessageFactory::create<RpcResponse>();
                msg->setId(req->rid());
                msg->setMType(bitpr::MType::RSP_RPC);
                msg->setRCode(rcode);
                msg->setResult(res);
                conn->send(msg);//发送
            }

            ServiceManager::ptr _service_manager;
        };

    }
    
}

